package tools {
	
	import entities.Entity;
	import entities.Player;
	import tools.MyBullet;
	import tools.MyFlxWeapon
	
	import flash.display.BitmapData;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxRect;
	import org.flixel.FlxSound;;
	
	public class WeaponManager extends FlxGroup {
		private static var weapon:MyFlxWeapon;
		private static var weaponInfos:Object;

		public function WeaponManager() {}
		
		public static function addWeapon(weaponName:String, shooter:Entity):MyFlxWeapon {
			weaponInfos = Registry.weaponInfos[weaponName];
			
			//shooter.infos.attack.meleeDamage = weaponInfos.power;
			
			//weapon properties
			var tileWidth:Number = 10;
			var tileHeight:Number = 10;
			var quantity:Number = 50;
			var image:BitmapData = weaponInfos.image;
			
			weapon = new MyFlxWeapon(weaponName, shooter,'x','y', weaponInfos.power);
			
			if(weaponInfos.sound) weapon.onFireSound = weaponInfos.sound;
			
			weapon.makeBitmapBullet(quantity, image, tileWidth, tileHeight, weaponInfos.frames );
			
			weapon.name = weaponName;
			weapon.setBulletBounds(new FlxRect(0, 0,  14000,  4000)); //  area limit
			weapon.setBulletLifeSpan(4000) //  life limit in ms for memory
			weapon.setFireRate(weaponInfos.delay * 1000); //	rate (delay in ms between 2 shot)									
			weapon.setBulletSpeed(weaponInfos.speed); //	speed (px/sec)								
			//weapon.setBulletGravity(0, weaponInfos.gravity) //  bullets weight
			weapon.setBulletGravity(0, Registry.gameInfos.world.gravity * weaponInfos.gravity ) //  bullets weight
			weapon.setBulletOffset(shooter.infos.attack.weaponOffset[0] ? shooter.infos.attack.weaponOffset[0] : 0, shooter.infos.attack.weaponOffset[1] ? shooter.infos.attack.weaponOffset[1] : 0)
				
			if(shooter is Player) Registry.friendlyPool.add(weapon.group);
			else Registry.hostilePool.add(weapon.group);
			return shooter.weapon = weapon;
		}
		
			
	}
}
